package tasks.task04;

import info.clearthought.layout.TableLayout;

import javax.persistence.Column;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class easytask04 {
    static ResultSet rs;

    public static void main(String[] args) {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            final Connection con = DriverManager.getConnection("jdbc:mysql://db4free.net/grishin", "grishin", "07051990");

            TableLayout tl = new TableLayout(new double[][]{{0, TableLayout.FILL, 0,2},
                    {0.2, TableLayout.FILL, 0.1}});

            final JFrame frame = new JFrame();
            frame.setSize(600, 400);
            frame.setLayout(tl);

            final JTable table = new JTable();

            frame.add(new JScrollPane(table), "0,0,1,1");

            final JButton loadbutton = new JButton("load");
            loadbutton.setBackground(Color.YELLOW);
            frame.add(loadbutton, "1,2");

            frame.setVisible(true);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            loadbutton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        Statement st = con.createStatement();
                        rs = st.executeQuery("SELECT * FROM user");
                        table.setModel(getTableModel());
                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//    public static void ChangeName(JTable table, int col_index, String col_name){
//            table.getColumnModel().getColumn(col_index).setHeaderValue(col_name);
//  }
    public static AbstractTableModel getTableModel() {
        return new AbstractTableModel() {
            public int getColumnCount() {
                return 3;
            }

            public int getRowCount() {
                int count = 0;
                try {
                    rs.last();
                    count = rs.getRow();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                return count;
            }

            public Object getValueAt(int row, int col) {
                String val = null;
                try {
                    rs.absolute(row + 1);
                    val = rs.getString(col + 1);


                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return val;
            }

            @Override
            public String getColumnName(int column) {
                if (column == 0) return "hello";
                if (column == 1) return "world";
                else return "!!!";                     
            }
        };
    }
}